const mammoth = require("mammoth");
const fs = require("fs");

function analyze(path) {
	return new Promise((resolve, reject) => {
		mammoth.extractRawText({ path: path })
			.then((result) => {
				const text = result.value;

				const lines = text.split("\n").filter(line => line.trim() !== "");
				// 可以用正则表达式提取关键信息
				const nameMatch = text.match(/姓\s*名\s*([^\n]+)/);
				const genderMatch = text.match(/性\s*别\s*([^\n]+)/);
				const nationMatch = text.match(/民\s*族\s*([^\n]+)/);
				const nativePlaceMatch = text.match(/籍\s*贯\s*([^\n]+)/);
				const birthDateMatch = text.match(/出生年月\s*([^\n]+)/);
				const birthPlaceMatch = text.match(/出生地\s*([^\n]+)/);

				const info = {
					name: nameMatch ? nameMatch[1].trim().replaceAll(' ', '').replaceAll("　",'') : "未填写",
					gender: genderMatch ? genderMatch[1].trim() : "未填写",
					nation: nationMatch ? nationMatch[1].trim() : "未填写",
					nativePlace: nativePlaceMatch ? nativePlaceMatch[1].trim() : "未填写",
					birthDate: lines[13] + lines[14] || "未填写",
					birthPlaceMatch: birthPlaceMatch ? birthPlaceMatch[1].trim() : "未填写",
					healthy: lines[28],
					xueli: lines[33],
					zy: lines[31],
					school: lines[37],
				};
				resolve(info)
			})
			.catch((err) => {
				reject(err);
				console.error("解析失败：", err);
			});
	})
}

module.exports = {
	analyze,
}
